package com.jy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.mapper.CheckGroupCheckItemMapper;
import com.jy.mapper.CheckGroupMapper;
import com.jy.pojo.CheckGroup;
import com.jy.pojo.CheckGroupCheckItem;
import com.jy.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.List;

@Service
public class CheckGroupService {
    @Autowired
    CheckGroupMapper checkGroupMapper;
    @Autowired
    CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    //事务:允许,把多条sql语句当作一个整体,要么全执行,要么一个不执行,回滚
    @Transactional
    public void add(CheckGroup checkGroup, List<Integer> checkitemIds) {
        // 1.添加本体表的内容,产生id
        checkGroupMapper.insert(checkGroup);
        // 2.通过产生的id,插入到中间表中
        //  由于前端传过来的id是checkitem的id
        //  而中间表中需要两个id,另一个id,添加之后已经产生了
        for (Integer checkitemId : checkitemIds) {
            Integer checkGroupId = checkGroup.getId();
            CheckGroupCheckItem cc = new CheckGroupCheckItem();
            cc.setCheckgroupId(checkGroupId);
            cc.setCheckitemId(checkitemId);
            checkGroupCheckItemMapper.insert(cc);
        }
    }

    public Page findPage(QueryPageBean qb) {
        Page<CheckGroup> page = new Page<>(qb.getCurrentPage(), qb.getPageSize());
        LambdaQueryWrapper<CheckGroup> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(qb.getQueryString())) {
            qw.eq(CheckGroup::getCode, qb.getQueryString());
            qw.or();
            qw.like(CheckGroup::getName, qb.getQueryString());
            qw.or();
            qw.eq(CheckGroup::getHelpCode, qb.getQueryString());
        }
        qw.orderByDesc(CheckGroup::getId);
        return checkGroupMapper.selectPage(page, qw);
    }

    public CheckGroup findById(Integer id) {
        return checkGroupMapper.selectById(id);
    }

    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupCheckItemMapper.findCheckItemIdsByCheckGroupId(id);
    }

    @Transactional
    public void edit(CheckGroup checkGroup, List<Integer> checkitemIds) {
        // 1.添加本体表的内容,产生id
        checkGroupMapper.updateById(checkGroup);

        // 2.先清空中间表的关联数据
        LambdaQueryWrapper<CheckGroupCheckItem> qw = new LambdaQueryWrapper<>();
        qw.eq(CheckGroupCheckItem::getCheckgroupId, checkGroup.getId());
        checkGroupCheckItemMapper.delete(qw);

        // 3.再把新的关联数据添加回去
        for (Integer checkitemId : checkitemIds) {
            Integer checkGroupId = checkGroup.getId();
            CheckGroupCheckItem cc = new CheckGroupCheckItem();
            cc.setCheckgroupId(checkGroupId);
            cc.setCheckitemId(checkitemId);
            checkGroupCheckItemMapper.insert(cc);
        }
    }

    @Transactional
    public void delete(Integer id) {
        //注意事项
        //1.先删除中间表中的关联数据
        LambdaQueryWrapper<CheckGroupCheckItem> qw = new LambdaQueryWrapper<>();
        qw.eq(CheckGroupCheckItem::getCheckgroupId, id);
        checkGroupCheckItemMapper.delete(qw);
        //2.在删除本体表中的数据
        checkGroupMapper.deleteById(id);
    }
}
